MATRIX

Section: Linear Algebra ()
Updated: June 12 1992
Index Return to Main Contents
 

NAME

Matrix<Type> - A parameterized matrix class.  

SYNOPSIS

#include <cool/Matrix.h>  

DESCRIPTION

The Matrix<Type> class implements two-dimensional arithmetic matrices for a user-specified numeric data type. Using the parameterized types facility of C++, it is possible, for example, for the user to create a matrix of rational numbers by parameterizing the Matrix class over the Rational class. The only requirement for the type is that it supports the basic arithmetic operators.

Note that unlike the other sequence classes, the Matrix<Type> class is fixed-size. It will not grow once the size has been specified to the constructor or changed by the assignment or multiplication operators. The Matrix<Type> class is row-based with addresses of rows being cached, and elements accessed as m[row][col].  

BASE CLASSES

Matrix.  

FRIEND CLASSES

M_Vector<Type>.  

CONSTRUCTORS

Matrix<Type> (unsigned int rows=1, unsigned int cols=1);
Allocates enough storage for a matrix of a specific type with the specified number of rows and columns. Elements are not initialized.
Matrix<Type> (unsigned int rows, unsigned int cols, const Type& value);
Allocates enough storage for a matrix of a specific type with the specified number of rows and columns. In addition, each element of the matrix is initialized to value.
Matrix<Type> (unsigned int rows, unsigned int cols, int init_num, Type v00, ...);
Allocates enough storage for a matrix of the specified type and size. The third argument init_num indicates the number of optional initialization values. Matrix elements are initialized in row-major order.

Arguments in ... can only be pointers, primitive types like int, double, and NOT objects, passed by reference or value, like vectors, matrices; because constructors must be known and called at compile time. Also, cannot have char or float in ...

Matrix<Type> (unsigned int rows, unsigned int cols, const Type* data_block);
Creates a matrix with specified dimension rows and cols, and initializes it with elements from the array data_block. The elements in data_block are stored row-wise in a contiguous block of memory.
Matrix<Type> (const Matrix<Type>& m);
Duplicates the size and value of matrix m.
~Matrix<Type> ();
Frees up storage for matrix arrays. Destructor on the elements is called only if Type denotes a class, and not a pointer.
 

MEMBER FUNCTIONS

Matrix<Type> abs () const;
Returns the matrix of the absolute values.
inline int columns () const;
Returns the number of columns in the matrix.
inline const Type* data_block ();
Returns the block of elements of the matrix. The elements are stored row-wise in a contiguous block of memory. Use this accessor only for read-only conversion to C array, like the type of m in: "Type m [rows][cols];".
Type determinant () const;
Returns the determinant of a square matrix using Cramer's rule. Signals an Eerror exception if the matrix is not square.
Matrix<Type> extract (unsigned int rows, unsigned int cols, unsigned int top=0, unsigned int left=0) const;
Returns a submatrix specified by the size and top-left corner.
void fill (const Type& value);
Sets all elements in the matrix to a specified fill value.
inline Type& get (unsigned int row, unsigned int col);
Returns the reference to the element at the indicated row and column. O(1) time. If the row or column specification is out of range, an Error exception is raised.
inline Type& operator () (unsigned int row, unsigned int col=0);
Overloads () to return the reference to the element at specified indexes. O(1) time. No out of range check is done.
Matrix<Type>& operator= (const Type& value);
Overloads the assignment operator and assigns all elements of a matrix to value.
Matrix<Type>& operator= (const Matrix<Type>& m);
Overloads the assignment operator and assigns lhs matrix to have the value of matrix m by duplicating the size and element values.
Matrix<Type> operator- () const;
Overloads the negation operator to negate all elements of source matrix.
Matrix<Type> operator+ (const Type& value) const;
inline Matrix<Type> operator- (const Type& value) const;
Matrix<Type> operator* (const Type& value) const;
Matrix<Type> operator/ (const Type& value) const;
Overloads respectively the addition, substraction, multiplication, division operator to add, substract, multiply, divide, all elements of lhs matrix with a scalar.
Matrix<Type>& operator+= (const Type& value);
inline Matrix<Type>& operator-= (const Type& value);
Matrix<Type>& operator*= (const Type& value);
Matrix<Type>& operator/= (const Type& value);
The elements of the lhs matrix are respectively added, substracted, multiplied, divided with value in place.
Matrix<Type>& operator+= (const Matrix<Type>& m);
Matrix<Type>& operator-= (const Matrix<Type>& m);
Overloads the addition and substraction -with-assignment operator. The lhs matrix is modified in place to contain respectively the matrix addition and substraction with m. If the dimensions of the first matrix is not equal to the dimensions of the second matrix, an Error exception is raised.
Matrix<Type>& operator*= (const Matrix<Type>& m);
Overloads the multiplication-with-assignment operator. The lhs matrix is modified to contain the matrix product. If the number of columns of the first matrix is not equal to the number of rows of the second matrix, an Error exception is raised.
Boolean operator== (const Matrix<Type>& m) const;
Overloads the equality operator. Returns TRUE if the matrices have the same number of elements with the same values.
inline Boolean operator!= (const Matrix<Type>& m) const;
Overloads the inequality operator. Returns TRUE if the matrices have a different number of elements or different values.
inline void put (unsigned int row, unsigned int col, Type& value);
Assigns value to the element at the specified row and col. If the row or column specification is out of range, an Error exception is raised.
inline int rows () const;
Returns the number of rows in the matrix.
void set_compare (Matrix_Compare = NULL );
Updates the compare function for this class of matrix. Matrix_Compare is a function of type Boolean (* Function)(const Type&, const Type&). If no argument is provided, the operator== for the type over which the class is parameterized is used.
Matrix<Type> sign () const;
Returns the matrix whose elements are either -1, 0, or 1, depending on whether the corresponding values are negative, 0, or positive.
inline Type& Matrix<Type> x ();
inline Type& Matrix<Type> y ();
inline Type& Matrix<Type> z ();
inline Type& Matrix<Type> t ();
x, y, z, t denote accessors for the coordinates of vector when the latter represents a column matrix.
Matrix<Type> transpose () const;
Returns the transpose of the source matrix.
Matrix<Type>& update (const Matrix<Type>& m,unsigned int top=0,unsigned int left=0);
Replaces the elements starting at top-left corner of the lhs matrix, by the elememts in matrix m. If the dimensions do not match, an Error exception is raised.
 

FRIEND FUNCTIONS

friend Type cross_2d (const Matrix<Type>& v1, const Matrix<Type>& v2);
Returns the cross-product of two 2d-vectors.
friend Matrix<Type> cross_3d (const Matrix<Type>& v1, const Matrix<Type>& v2);
Returns the 3x1 cross-product of two 3d-vectors.
friend Type dot_product (const Matrix<Type>& v1, const Matrix<Type>& v2);
Returns the dot product of two vectors.
friend Matrix<Type> element_product (const Matrix<Type>& m1, const Matrix<Type>& m2);
Returns the matrix whose elements are the products m1(i,j) * m2(i,j).
friend Matrix<Type> element_quotient (const Matrix<Type>& m1, const Matrix<Type>& m2);
Returns the matrix whose elements are the quotients m1(i,j) / m2(i,j).
inline friend Matrix<Type> operator+ (const Type& value, const Matrix<Type>& m);
inline friend Matrix<Type> operator- (const Type& value, const Matrix<Type>& m);
inline friend Matrix<Type> operator* (const Type& value, const Matrix<Type>& m);
Overloads respectively the addition, substraction, multiplication operator to add, substract, multiply the elements of m with scalar value.
inline friend Matrix<Type> operator+ (const Matrix<Type>& m1, const Matrix<Type>& m2);
inline friend Matrix<Type> operator- (const Matrix<Type>& m1, const Matrix<Type>& m2);
inline friend Matrix<Type> operator* (const Matrix<Type>& m1, const Matrix<Type>& m2);
Overloads respectively the addition, substraction, multiplication operator to add, substract, multiply two matrices. If the number of rows and columns do not match, an Error exception is raised.
friend ostream& operator<< (ostream& os, const Matrix<Type>& m);
inline friend ostream& operator<< (ostream& os, const Matrix<Type>* m);
Overloads the output operator for a reference or a pointer to a Matrix<Type> object to provide a formatted output capability.
 

EXAMPLE


  #include <cool/Matrix.h>                  


  int main (void) {
    Matrix<int> mc1(3,4),mc2(3,4);          
    for (int i = 0; i < 3; i++)             
      for (int j = 0; j < 4; j++)           
        mc1.put(i,j,(i+2)*(j+3));           
    mc2 = mc1 + 5;                          
    mc1 = mc1 + mc2;                        
    cout << mc1 << " << mc2;             
    return (0);                                                
  }

The output from above is:
  17 21 25 29 
  23 29 35 41 
  29 37 45 53 


  11 13 15 17 
  14 17 20 23 
  17 21 25 29   

SEE ALSO

M_Vector<Type> implements one-dimensional version of Matrix<Type>.

Envelope allows efficient return by value in arithmetic expressions.  

COPYRIGHT

Copyright (C) 1991 Texas Instruments Incorporated.

Permission is granted to any individual or institution to use, copy, modify, and distribute this software, provided that this complete copyright and permission notice is maintained, intact, in all copies and supporting documentation.

Texas Instruments Incorporated provides this software "as is" without express or implied warranty.


 

Index

NAME
SYNOPSIS
DESCRIPTION
BASE CLASSES
FRIEND CLASSES
CONSTRUCTORS
MEMBER FUNCTIONS
FRIEND FUNCTIONS
EXAMPLE
SEE ALSO
COPYRIGHT

This document was created by man2html, using the manual pages.
Time: 00:40:36 GMT, March 30, 2022